# @file    : dule_color_led
# @time    : 2025/3/13
# @author  : yongpeng.yao
# @desc    :
import RPi.GPIO as GPIO
import time

colors = [0xFF00, 0x00FF, 0x0FF0, 0xF00F]
pins = [11, 12]

GPIO.setmode(GPIO.BOARD)
GPIO.setup(pins, GPIO.OUT)
GPIO.output(pins, GPIO.LOW)

p_R = GPIO.PWM(pins[0], 2000)
p_G = GPIO.PWM(pins[1], 2000)

p_R.start(0)
p_G.start(0)


def map_(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min


def set_color(color):
    r_val = color >> 8
    g_val = color & 0x00FF

    r_val = map_(r_val, 0, 255, 0, 100)
    g_val = map_(g_val, 0, 255, 0, 100)

    p_R.ChangeDutyCycle(r_val)
    p_G.ChangeDutyCycle(g_val)


def loop():
    while True:
        for color in colors:
            set_color(color)
            time.sleep(0.5)


def destroy():
    p_R.stop()
    p_G.stop()
    GPIO.output(pins, GPIO.LOW)
    GPIO.cleanup()


if __name__ == '__main__':
    try:
        loop()
    except KeyboardInterrupt:
        destroy()
